// pages/inparam/fs/in_hospitalIncome/in_hospitalIncome.js
// pages/inpram/drug/in_DrugConsumption/in_DrugConsumption.js
// pages/inpram/drug/in_TreasuryDrugs/in_TreasuryDrugs.js
import { $wuxCalendar } from '../../../../dist/index'
const network = require('../../../../utils/network.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value1: ['2020-01-01'],
    value2: ['2020-01-30'],
    //选择库存地点之后的参数
    pramLocation: {
      dept: '',
      deptName: '请选择'
    },
    pramFeeType: {
      type: '',
      typeName: '请选择'
    },
    //科室列表
    locationList: [
      {
        dept:'0',
        deptName:'全院科室'
      },
      {
        dept: '1',
        deptName: '门诊科室'
      },
      {
        dept: '2',
        deptName: '住院科室'
      }
    ],
    //费用分类
    feeTypeList:[
      {
        type: '0',
        typeName: '全部收入'
      },
      {
        type: '1',
        typeName: '药品收入'
      },
      {
        type: '2',
        typeName: '医疗收入'
      }
    ]
  },


  /**
     * 生命周期函数--监听页面加载
     */
  onLoad: function (options) {
    var that = this
    // //调用封装好的网络请求，获取数据
    // network.get('/utils/cldrugdept', {}
    // ).then(function (res) {
    //   console.log('res:', res)
    //   var obj1 = res.data.data

    //   that.setData({
    //     //'locationList': obj1,
    //   })
    // }).then(function (res) {

    // }).catch(function (res) {
    //   console.log(res)
    // })


  },

  // 开始时间选择  
  openCalendar1() {
    $wuxCalendar().open({
      value: this.data.value1,
      onChange: (values, displayValues) => {
        console.log('onChange', values, displayValues)
        this.setData({
          value1: displayValues,
        })
      },
    })
  },

  // 结束时间选择  
  openCalendar2() {
    $wuxCalendar().open({
      value: this.data.value2,
      // multiple: true,
      onChange: (values, displayValues) => {
        console.log('onChange', values, displayValues)
        this.setData({
          value2: displayValues,
        })
      },
    })
  },


  bindPickerChange_location: function (e) {
    let that = this;
    var nowData = [];
    nowData = that.properties.locationList;//当前option的数据是引入组件的页面传过来的，所以这里获取数据只有通过this.properties
    var nowIdx = e.target.dataset.index;//当前点击的索引
    that.setData({   //给变量赋值
      //dept_index: e.detail.value,  //每次选择了下拉列表的内容同时修改下标然后修改显示的内容，显示的内容和选择的内容一致
      'pramLocation.dept': nowData[e.detail.value].dept,
      'pramLocation.deptName': nowData[e.detail.value].deptName

    })

  },
  bindPickerChange_feeType: function (e) {
    let that = this;
    var nowData = [];
    nowData = that.properties.feeTypeList;//当前option的数据是引入组件的页面传过来的，所以这里获取数据只有通过this.properties
    var nowIdx = e.target.dataset.index;//当前点击的索引

    that.setData({   //给变量赋值

      'pramFeeType.type': nowData[e.detail.value].type,
      'pramFeeType.typeName': nowData[e.detail.value].typeName

    })

  },


  // 时间比较函数
  compareDate: function (startDate, endDate) {
    var arrStart = startDate.split("-");
    var startTime = new Date(arrStart[0], arrStart[1], arrStart[2]);
    var startTimes = startTime.getTime();
    var arrEnd = endDate.split("-");
    var endTime = new Date(arrEnd[0], arrEnd[1], arrEnd[2]);
    var endTimes = endTime.getTime();
    if (endTimes <= startTimes) {
      return false;
    }
    return true;
  },

  /**
   * 检查参数是否合法，跳转
   */
  determine: function () {
    var that = this
    var day = that.compareDate(that.data.value1[0], that.data.value2[0]);
    console.log('day', day)
    if (!day) {
      wx.showModal({

        title: '提示',

        content: '开始时间必须小于结束时间一天',

      })

    } else if (Object.keys(that.data.pramLocation.dept).length === 0) {
      wx.showModal({

        title: '提示',

        content: '科室不能为空，请选择科室',

      })
    } else {
      wx.redirectTo({
        url: '../../../view/fs/hospitalIncome/hospitalIncome?startTime=' + that.data.value1[0] + '&endTime=' + that.data.value2[0] + '&dept=' + that.data.pramLocation.dept + '&type=' + that.data.pramFeeType.type
      })
    }
  },
  cancel: function () {
    wx.navigateBack({ changed: true });
  }
})